
/**
 * @module Promiseify wx.request
 */

``

class BaseRequest{


    constructor(opt){
        
        this.defaultOpt = {

            baseurl: '',
            token: '',
            headToken: false,
            cookieToken: false,
            queryToken: false,
            upFile: false
    
        }
        
        
        if(opt){
            this.defaultOpt = this._createOpt(opt);
        }
    }

    _createOpt(opt){

        let defaultOpt = {...this.defaultOpt};

        if(opt){
            Object.keys(defaultOpt).forEach(key =>{
                let same = (typeof opt[key]) === (typeof defaultOpt[key]);
                if(same){
                    defaultOpt[key] = opt[key]
                }else{
                    if(opt[key] !== undefined){
                        throw new Error(`请求参数类型错误: ${key}, 预期类型${typeof defaultOpt[key]}`)
                    }
                }
            })
        };

        return defaultOpt;
    }
    
    
    request (obj, opt){
        
        let req =  this._formRequestObj(obj, opt);
        
        if(req){
            
            return new Promise((resolve, reject) =>{
                
                let queryData = {success: resolve, fail: reject, ...obj};
                wx.request(queryData)
                
            }).then(res =>{

                if(res.statusCode === 200){
                    return res.data
                }else{
                    return Promise.reject(res)
                }

            })
        }else{

            return Promise.reject({
                errorMsg: '无法格式化请求参数',
                obj,
                opt
            })

        }

    }

    _formRequestObj (obj, opt){
        opt = this._createOpt(opt)
        obj.url = this._getFullUrl(obj.url);        
        
        try{

            if(opt.headToken){
                obj.url = `${obj.url}?${opt.tokenKey}=${opt.token}`
            }
   
            if(opt.cookieToken){
                obj.header = Object.assign({}, obj.header, {
                    Cookie: `${opt.tokenKey}=${ opt.token || ''}` 
                })
            }

            if(opt.headToken){
                obj.header = Object.assign({}, obj.header, {
                    Authorization: `Bearer ${opt.token}`
                })
            }
            
            if(opt.upFile){
                obj.header = Object.assign({}, obj.header, {
                    "Content-Type": "multipart/form-data"
                })
            }

            
        }catch(e){
            console.log(e)
            return false;
        }
        
        return obj;
        
    }

    _getFullUrl(url){
        url = url.indexOf('/') === 0 ? url : '/' + url;
        return this._urlTest(url) ? url : this.defaultOpt.baseurl + url; 
    }

    _urlTest(url){
        return /^https?:\/\//i.test(url)
    }
    
}


class Request extends BaseRequest{

    constructor (opt){
        super(opt)
    }

    GET(obj, opt){

        obj = {
            ...obj,
            method: 'GET'
        }
        
        return this.request(obj, opt)
    }

    POST(obj, opt){
        obj = {
            ...obj,
            method: 'POST'
        }
        
        return this.request(obj, opt)
    }

    PUT (obj, opt){
        obj = {
            ...obj,
            method: 'PUT'
        }
        
        return this.request(obj, opt)
    }

    DELETE (obj, opt){
        obj = {
            ...obj,
            method: 'DELETE'
        }
        
        return this.request(obj, opt)
    }
    
    
}

module.exports = Request